package nio;

import nio.util.BaseUtil;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioDemo3 {

    public static void main(String[] args) {
        demo1();
    }

    /**
     * Scatter/Gather
     */
    private static void demo1(){
        try (FileChannel channel = new RandomAccessFile("niodemo1.txt","rw").getChannel()){
            BaseUtil.logSwitch(false);

            ByteBuffer buffer1 = ByteBuffer.allocate(10);
            ByteBuffer buffer2 = ByteBuffer.allocate(5);

            ByteBuffer[] buffers =  {buffer1,buffer2};
            channel.read(buffers);

            buffer1.flip();
            buffer2.flip();

            BaseUtil.printByteBuffer(buffer1);
            BaseUtil.printByteBuffer(buffer2);

            buffer1.flip();
            buffer2.flip();

            channel.write(buffers);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
